package com.laozhao.msas.util;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import jxl.Cell;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class ReportUtil {
	
	public static Map<String, String> getDefalultParamer() {
		Map<String, String> paramer = new HashMap<String, String>(20);
//		paramer.put("systemDate", new Date());
		return paramer;
	}

	public static String fillReport(Map<String, String> paramer,
			String templatePath, String sheetName, String reportPath,
			String templatePassword, String reportPassword) throws IOException {
		File templateFile = new File(templatePath);
		File reportFile = new File(reportPath);
		try {
			Workbook templateWorkbook = Workbook.getWorkbook(templateFile
					.getAbsoluteFile());
			WritableWorkbook reportWorkbook = Workbook
					.createWorkbook(reportFile.getAbsoluteFile(),templateWorkbook);
			// reportWorkbook.
//			 rwb.close();
//			Sheet templateSheet;
//			if (sheetName != null) {
//				templateSheet = templateWorkbook.getSheet(sheetName);
//			} else {
//				templateSheet = templateWorkbook.getSheet(0);
//			}
//			if (templatePassword != null) {
//				SheetSettings ss = templateSheet.getSettings();
//				ss.setPassword(templatePassword);
//				ss.setProtected(false);
//			}
			
//			WritableSheet reportSheet = reportWorkbook.createSheet(
//					templateSheet.getName(), 0);
//			WritableSheet reportSheet = reportWorkbook.importSheet(templateSheet.getName(), 0, templateSheet);

			WritableSheet reportSheet;
			if (sheetName != null) {
				reportSheet = reportWorkbook.getSheet(sheetName);
			} else {
				reportSheet = reportWorkbook.getSheet(0);
			}
//			reportWorkbook.
			
			Cell cell ;
			for (int row = 0; row < reportSheet.getRows(); row++) {
				for (int column = 0; column < reportSheet.getColumns(); column++) {
					cell = reportSheet.getWritableCell(column, row);
					if (!cell.isHidden()) {
						if (paramer.containsKey(cell.getContents())) {
//							reportSheet.addCell(parseCell(column, row, paramer.get(cell.getContents())));
							Label label = (Label)cell;
							label.setString(paramer.get(cell.getContents()));
						}
					}
				}
			}


			if (reportPassword != null) {
				SheetSettings ss = reportSheet.getSettings();
				ss.setPassword(reportPassword);
				ss.setProtected(true);
			}

			reportWorkbook.write();
			reportWorkbook.close();
			reportWorkbook = null;
			
			templateWorkbook.close();
			templateWorkbook = null;
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}
		return reportFile.getAbsolutePath();
	}

	public static void main(String[] args) {
		Map<String, String> paramer = new HashMap<String, String>(2);
		paramer.put("#hello#", "i am a very storg man!");
		String templatePath = "D:\\delivery.xls";
		String reportPath = "D:\\delivery-report.xls";
		try {
			ReportUtil.fillReport(paramer, templatePath, null, reportPath, null, null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
